/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.project.controller;

import edu.project.annotations.ColumnAnnotation;
import edu.project.annotations.TableAnnotation;
import edu.project.model.SqlSyntax;
import edu.project.model.Student;
import java.lang.reflect.Field;

/**
 *
 * @author DoHongPhuc
 */
public class SqlGenerator {

    public String generateSelectAll(Class c) {
        SqlSyntax sqlSyntax = new SqlSyntax();
        TableAnnotation tableAnnotation = (TableAnnotation) c.getAnnotation(TableAnnotation.class);
        sqlSyntax.select("*").from(tableAnnotation.name());

//        StringBuffer column = new StringBuffer();
//        for (Field field : c.getFields()) {
//            ColumnAnnotation columnAnnotation = (ColumnAnnotation) field.getAnnotation(ColumnAnnotation.class);
//            column.append(columnAnnotation.name() + ",");
//        }
//        column.deleteCharAt(column.length() - 1);
        return sqlSyntax.toString();
    }

    public String generateInsert(Class c) {
        SqlSyntax sqlSyntax = new SqlSyntax();
        TableAnnotation tableAnnotation = (TableAnnotation) c.getAnnotation(TableAnnotation.class);
        StringBuffer column = new StringBuffer();

        for (Field field : c.getFields()) {
            ColumnAnnotation columnAnnotation = (ColumnAnnotation) field.getAnnotation(ColumnAnnotation.class);
        }
        //column.deleteCharAt(column.length() - 1);
        return column.toString();
    }

    public static void main(String[] args) {
        System.out.println(new SqlGenerator().generateSelectAll(new Student().getClass()));
        Student st = new Student();
        System.out.println(new SqlGenerator().generateInsert(st.getClass()));

    }
}
